package org.ym.database.hibernate.base;

import com.google.gson.annotations.JsonAdapter;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.hibernate.annotations.GenericGenerator;
import org.ym.database.hibernate.listener.AuditingEntityListener;
import org.ym.tools.adapter.LongJsonAdapter;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;

/**
 * @author ym
 * 描述： @EntityListeners需配合@EnableJpaAuditing使用
 */
@Getter
@Setter
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
@EqualsAndHashCode
@ToString
public abstract class BaseModel implements Serializable {
    private static final long serialVersionUID = 1L;
    /**
     * 主键
     */
    @Id
    @Column(name = "id",unique = true,nullable = false,updatable = false)
    @GenericGenerator(name = "SnowflakeIdGenerator",strategy = "org.ym.database.hibernate.utils.SnowflakeIdGenerator")
    @GeneratedValue(generator = "SnowflakeIdGenerator")
    @JsonAdapter(LongJsonAdapter.class)
    private Long id;

    /**
     * 创建时间
     */
    @Column(name = "create_time",updatable = false)
    @Temporal(TemporalType.TIMESTAMP)
    private Date createTime;

    /**
     * 创建人主键
     */
    @Column(name = "creator_by_id",updatable = false)
    private Long createdById;

    /**
     * 创建人主键
     */
    @Column(name = "creator_by",length = 32,updatable = false)
    private String createdBy;

    /**
     * 更新时间
     */
    @Column(name = "update_time",insertable = false)
    @Temporal(TemporalType.TIMESTAMP)
    private Date updateTime;

    /**
     * 更新人主键
     */
    @Column(name = "updated_by_id",insertable = false)
    private Long updatedById;

    /**
     * 更新人主键
     */
    @Column(name = "updated_by",length = 32,insertable = false)
    private String updatedBy;
    /**
     * 用于并发
     */
    @Version
    @Column(name = "version")
    private Integer version;
    /**
     * 删除标记
     */
    @Column(name = "deleted")
    private Integer deleted;
}
